package com.zjw.demo.vertx.starter.plugin;

import com.zjw.demo.vertx.starter.annotation.core.Plugin;
import com.zjw.demo.vertx.starter.config.ApplicationConfig;
import com.zjw.demo.vertx.starter.core.Reflector;
import com.zjw.demo.vertx.starter.utils.LogUtils;

import java.util.List;

public class PluginDispatcher {

  private static final String TAG = "PluginDispatcher";


  public static void dispatchPlugin() {
    List<Class<?>> pluginList = Reflector.reflectPackageAnnotationClass(ApplicationConfig.PLUGIN_PATH, Plugin.class);

    for (Class<?> aClass : pluginList) {
      LogUtils.debug(TAG, "start " + aClass.getName());
      Object o = Reflector.instanceEmptyConstructor(aClass);
      if (o == null) {
        throw new IllegalArgumentException("@Plugin class must have empty constructor :" + aClass.getName());
      }

      if (!(o instanceof IPlugin)) {
        throw new IllegalArgumentException("@Plugin class must impl IPlugin :" + aClass.getName());
      }

      ((IPlugin) o).onPlugin(Reflector.vertx);

      LogUtils.debug(TAG, "end " + aClass.getName());
    }
  }
}
